package com.example.service;

import cn.hutool.core.date.DateUtil;
import com.example.entity.Notice;
import com.example.mapper.NoticeMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 查询所有公告信息
     * @return 公告列表
     */
    public List<Notice> selectAll(Notice notice) {
        return noticeMapper.selectAll(notice);
    }

    /**
     * 分页查询公告信息
     * @param pageNum   页码
     * @param pageSize  每页显示数量
     * @return 分页信息
     */
    public PageInfo<Notice> selectByPage(Notice notice, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Notice> notices = noticeMapper.selectAll(notice);
        return PageInfo.of(notices);
    }

    public void add(Notice notice) {
        notice.setTime(DateUtil.now());
        noticeMapper.add(notice);
    }

    public void update(Notice notice) {
        noticeMapper.updateById(notice);
    }

    public void delete(Integer id) {
        noticeMapper.deleteById(id);
    }

}
